import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import session from 'express-session';
import ms from 'ms';

@Injectable()
export class SessionMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction): void {
    console.log('请求session中间件...');
    session({
      name: 'MY_SESSION',
      secret: `some-random-secret-here`,
      resave: true,
      saveUninitialized: false,
      rolling: true,
      cookie: {
        secure: process.env.NODE_ENV === 'production',
        maxAge: ms('5m'),
      },
    })(req, res, next);
  }
}
